package cn.zspt.fingerJoy.controller;

import cn.zspt.fingerJoy.domain.Seller;
import cn.zspt.fingerJoy.domain.Theme;
import cn.zspt.fingerJoy.domain.User;
import cn.zspt.fingerJoy.service.FingerJoyService;
import cn.zspt.fingerJoy.util.JwtUtils;
import cn.zspt.fingerJoy.util.MsgEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
   
@RestController
public class FingerController {
    @Autowired
    private FingerJoyService fingerJoyService;
    @RequestMapping("/")
    public String home(@RequestHeader("Authorization") String token){
        System.out.println(JwtUtils.getUid(token));
        return "hello";
    }

    @PostMapping(value = "/login",produces = "application/json;charset=utf-8")
    public Map<String,Object> login(@RequestBody User user){
        System.out.println(user);
//        if(user.getUserName().equals("admin")&&user.getPassword().equals("123")){
//            user.setId(1);
//            Map<String, String> tokenmap= new HashMap<>();
//            tokenmap.put("username", user.getUserName());
//            tokenmap.put("uid", user.getId()+"");
//            String token= JwtUtils.getToken(tokenmap);
//            return token;
//        }

        User dbUser=fingerJoyService.login(user);
        System.out.println(dbUser);
        if(dbUser!=null){
            Map<String, String> tokenmap= new HashMap<>();
            tokenmap.put("username", dbUser.getUserName());
            tokenmap.put("uid", dbUser.getId().toString());
            String token= JwtUtils.getToken(tokenmap);

            Map<String,Object> map=new HashMap<>();
            map.put("code", MsgEnum.OPER_SUCCESS.getCode());
            map.put("msg",MsgEnum.OPER_SUCCESS.getMsg());
            map.put("token",token);
            return map;
        }else{
            Map<String,Object> map=new HashMap<>();
            map.put("code", MsgEnum.LOGIN_FAIL.getCode());
            map.put("msg",MsgEnum.LOGIN_FAIL.getMsg());
            map.put("token","");
            return map;
        }
    }

    @GetMapping("/takeout/theme/list")
    public Map<String,Object> findThemeList(){
        List<Theme> themeList=fingerJoyService.findAllTheme();
        Map<String,Object> map=new HashMap();
        map.put("code", MsgEnum.OPER_SUCCESS.getCode());
        map.put("msg",MsgEnum.OPER_SUCCESS.getMsg());
        map.put("rows",themeList);
        return map;
    }

    @GetMapping("/takeout/seller/near")
    public Map<String,Object> findSellerNear(@RequestParam(defaultValue = "1")int pageNum
            ,@RequestParam(defaultValue = "8")int pageSize){
        PageInfo<Seller> sellerPageInfo=fingerJoyService.findSellerNear(pageNum,pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("code", MsgEnum.QUERY_SUCCESS.getCode());
        map.put("msg",MsgEnum.QUERY_SUCCESS.getMsg());
        map.put("total",sellerPageInfo.getTotal());
        map.put("rows",sellerPageInfo.getList());
        return map;
    }

    @GetMapping("/takeout/seller/{id}")
    public Map<String,Object> findSellerById(@PathVariable int id){
        Seller seller=fingerJoyService.findSellerById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("code", MsgEnum.OPER_SUCCESS.getCode());
        map.put("msg",MsgEnum.OPER_SUCCESS.getMsg());
        map.put("data",seller);
        return map;
    }

    @GetMapping("/takeout/search")
    public Map<String,Object> findSellerNear(@RequestParam(defaultValue = "1")int pageNum
            ,@RequestParam(defaultValue = "8")int pageSize,String keyword){
        PageInfo<Seller> sellerPageInfo=fingerJoyService.findSellerByKeyword(pageNum,pageSize,keyword);
        Map<String,Object> map=new HashMap<>();
        map.put("code", MsgEnum.QUERY_SUCCESS.getCode());
        map.put("msg",MsgEnum.QUERY_SUCCESS.getMsg());
        map.put("total",sellerPageInfo.getTotal());
        map.put("rows",sellerPageInfo.getList());
        return map;
    }


}
